package org.example.y24.m12;

public class LaGeLangRi {
    private static double lagrange(double x, double[] xNodes, int i) {
        double result = 1.0;
        for (int j = 0; j < xNodes.length; ++j) {
            if (j != i) {
                result *= (x - xNodes[j]) / (xNodes[i] - xNodes[j]);
            }
        }
        return result;
    }
    public static double lagrangeInterpolation(double[] xNodes, double[] yValues, double x) {
        double result = 0.0;
        for (int i = 0; i < xNodes.length; ++i) {
            result += yValues[i] * lagrange(x, xNodes, i);
        }
        return result;
    }
    public static void main(String[] args) {
        double[] xNodes = {-5, 3, 2.5, 2, -1.25};
        double[] yValues = {3, 9, -15.0/7, 2.0/3, 35};
        double x = 1.21;
        double result = lagrangeInterpolation(xNodes, yValues, x);
        System.out.println("f（1.21）的近似值为: " + result);
    }
}
